﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM.Interface
{
    public partial class frmKhuyenMaiMoi : frmTemplete1
    {
        int IDKhuyenMai;
        clsKhuyenMaiBUS KhuyenMaiBUS;
        bool InsertFlag = false;
        public frmKhuyenMaiMoi()
        {
            InitializeComponent();
            lblTitle.Text = "QUẢN LÝ KHUYẾN MÃI";
            uct_Reset.Reset.Click += new EventHandler(btn_Reset_Click);
            uct_Save.Save.Click += new EventHandler(btn_Save_Click);
            uct_Delete.Delete.Click += new EventHandler(btn_Delete_Click);
            uct_Refresh.Refresh.Click += new EventHandler(btn_Refresh_Click);
            IDKhuyenMai = 0;
            InsertFlag = false;
            KhuyenMaiBUS = new clsKhuyenMaiBUS();
            LoadData();
        }

        #region Functions

        private void SetPermission()
        {
            uct_Reset.Enabled = clsUser.LayThongTinPhanQuyen("KhuyenMai").ChucNang.Them;
            uct_Save.Enabled = clsUser.LayThongTinPhanQuyen("KhuyenMai").ChucNang.Sua;
            uct_Delete.Enabled = clsUser.LayThongTinPhanQuyen("KhuyenMai").ChucNang.Xoa;
        }

        #region LoadData()
        public void LoadData()
        {
            try
            {
                dtg_Data.Rows.Clear();
                DataTable dt = KhuyenMaiBUS.LayBang();
                DataView dv = new DataView(dt);
                dv.Sort = "KhuyenMaiId";
                for (int i = 0; i < dv.Count; i++)
                {
                    dtg_Data.Rows.Add(new object[] {"", dv[i]["KhuyenMaiId"], dv[i]["MaKhuyenMai"], 
                        dv[i]["TenKhuyenMai"], dv[i]["MoTa"], 
                        dv[i]["NgayBatDau"], dv[i]["NgayKetThuc"], dv[i]["SoTien"], dv[i]["PhanTram"] });
                }
            }
            catch { }
        }
        #endregion

        #region LayThongTinKhuyenMai(int KhuyenMaiId)
        public void LayThongTinKhuyenMai(int KhuyenMaiId)
        {
            try
            {
                clsKhuyenMaiDTO KhuyenMaiDTO = KhuyenMaiBUS.LayThongTinKhuyenMai(KhuyenMaiId);
                FillData(KhuyenMaiDTO);
            }
            catch{ }
        }
        #endregion

        #region FillData(clsKhuyenMaiDTO KM)
        public void FillData(clsKhuyenMaiDTO KM)
        {
            txtMaKhuyenMai.Text = KM.MaKhuyenMai;
            txtTenKhuyenMai.Text = KM.TenKhuyenMai;
            txtMoTa.Text = KM.MoTa;
            txtSTT.Text = KM.SoThuTu.ToString();
            txtPhanTram.Text = int.Parse(KM.PhanTram.ToString()) == 0 ? "" : KM.PhanTram.ToString();
            txtSoTien.Text = double.Parse(KM.SoTien.ToString()) == 0 ? "" : KM.SoTien.ToString();
            txtDNgayBatDau.Text = KM.NgayBatDau.ToShortDateString() == "1/1/0001" ? "__/____" : KM.NgayBatDau.ToShortDateString();
            txtDNgayKetThuc.Text = KM.NgayKetThuc.ToShortDateString() == "1/1/0001" ? "__/____" : KM.NgayKetThuc.ToShortDateString();
        }
        #endregion

        #region ClearData()
        public void ClearData()
        {
            txtMaKhuyenMai.Text = string.Empty;
            txtTenKhuyenMai.Text = string.Empty;
            txtMoTa.Text = string.Empty;
            txtSTT.Text = string.Empty;
            txtPhanTram.Text = string.Empty;
            txtSoTien.Text = string.Empty;
            txtDNgayBatDau.Text = string.Empty;
            txtDNgayKetThuc.Text = string.Empty;
        }
        #endregion
        #endregion

        #region Events
        #region btn_Reset_Click(object sender, EventArgs e)
        private void btn_Reset_Click(object sender, EventArgs e)
        {
            ClearData();
            txtMaKhuyenMai.Focus();
            InsertFlag = true;
        }
        #endregion

        #region btn_Save_Click(object sender, EventArgs e)
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if(InsertFlag)
            {
                if (MessageBox.Show("Xác nhận thêm dữ liệu?", "CMS_GYM", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    return;
                if (txtMaKhuyenMai.Text == string.Empty || txtTenKhuyenMai.Text == string.Empty)
                {
                    MessageBox.Show("Kiểm tra dữ liệu nhập vào", "CMS_GYM");
                    return;
                }
                clsKhuyenMaiDTO KhuyenMaiDTO = new clsKhuyenMaiDTO();
                KhuyenMaiDTO.KhuyenMaiId = IDKhuyenMai;
                KhuyenMaiDTO.MaKhuyenMai = txtMaKhuyenMai.Text;
                KhuyenMaiDTO.TenKhuyenMai = txtTenKhuyenMai.Text;
                KhuyenMaiDTO.MoTa = txtMoTa.Text;
                KhuyenMaiDTO.NgayBatDau = DateTime.Parse(txtDNgayBatDau.Text.Trim());
                KhuyenMaiDTO.NgayKetThuc = DateTime.Parse(txtDNgayKetThuc.Text.Trim());
                KhuyenMaiDTO.SoTien = double.Parse(txtSoTien.Text);
                KhuyenMaiDTO.PhanTram = int.Parse(txtPhanTram.Text);
                int i = KhuyenMaiBUS.Them(KhuyenMaiDTO);
                if (i == 1)
                {
                    MessageBox.Show("Thêm dữ liệu thành công", "CMS_GYM");
                    LoadData();
                    InsertFlag = false;
                }
                else
                    MessageBox.Show("Lỗi thêm dữ liệu", "CMS_GYM");
                return;
            }
            if (IDKhuyenMai == 0) return;
            if (MessageBox.Show("Xác nhận cập nhật dữ liệu?", "CMS_GYM", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                clsKhuyenMaiDTO KhuyenMaiDTO = new clsKhuyenMaiDTO();
                KhuyenMaiDTO.KhuyenMaiId = IDKhuyenMai;
                KhuyenMaiDTO.MaKhuyenMai = txtMaKhuyenMai.Text;
                KhuyenMaiDTO.TenKhuyenMai = txtTenKhuyenMai.Text;
                KhuyenMaiDTO.MoTa = txtMoTa.Text;
                KhuyenMaiDTO.NgayBatDau = DateTime.Parse(txtDNgayBatDau.Text.Trim());
                KhuyenMaiDTO.NgayKetThuc = DateTime.Parse(txtDNgayKetThuc.Text.Trim());
                KhuyenMaiDTO.SoTien = double.Parse(txtSoTien.Text);
                KhuyenMaiDTO.PhanTram = int.Parse(txtPhanTram.Text);
                int i = KhuyenMaiBUS.Sua(KhuyenMaiDTO);
                if (i == 1)
                {
                    MessageBox.Show("Cập nhật dữ liệu thành công", "CMS_GYM");
                }
                else
                    MessageBox.Show("Lỗi cập nhật dữ liệu", "CMS_GYM");
                LoadData();
            }
        }
        #endregion

        #region btn_Delete_Click(object sender, EventArgs e)
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (IDKhuyenMai == 0) return;
            if (MessageBox.Show("Xác nhận xóa dữ liệu?", "CMS_GYM", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
            {
                int i = KhuyenMaiBUS.Xoa(IDKhuyenMai);
                if (i == 1)
                {
                    MessageBox.Show("Xóa dữ liệu thành công", "CMS_GYM");
                }
                else
                    MessageBox.Show("Lỗi xóa dữ liệu", "CMS_GYM");
                LoadData();
            }
        }
        #endregion

        #region btn_Refresh_Click(object sender, EventArgs e)
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        private void dtg_Data_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                IDKhuyenMai = int.Parse(dtg_Data.SelectedRows[0].Cells["KhuyenMaiId"].Value.ToString());
                LayThongTinKhuyenMai(int.Parse(dtg_Data.SelectedRows[0].Cells["KhuyenMaiId"].Value.ToString()));
            }
            catch { }
        }
        #endregion

        private void frmKhuyenMaiMoi_Load(object sender, EventArgs e)
        {
            try
            {
                SetPermission();
            }
            catch { }
        }
    }
}
